package cn.wolfcode.Controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.meiju.AppointmentStatusEnum;
import cn.wolfcode.qo.LogicException;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;


    @RequirePermission(name = "预约页面",experssion = "business:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        AppointmentStatusEnum[] statusEnums = AppointmentStatusEnum.values();
        model.addAttribute("statusEnums", statusEnums);
        model.addAttribute("pageInfo", pageInfo);
        return "appointment/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "预约删除",experssion = "business:delete")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            appointmentService.delete(id);
        }
        return new jsonResult();
    }


    //后台新增或者修改预约
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "预约新增/修改",experssion = "business:saveOrUpdate")
    public String saveOrUpdate(Appointment appointment){

        if (appointment.getId() != null) {
            appointmentService.update(appointment);
        }else {
            appointmentService.save(appointment);
        }
        return "redirect:/appointment/list";
    }


    //页面添加预约
    @RequestMapping("/save")
    @ResponseBody
    public jsonResult save(Appointment appointment){
        try {
            appointmentService.save2(appointment);
        } catch (Exception e) {
            return new jsonResult(false,"预约失败");
        }
        return  new jsonResult(true,"预约成功");
    }



    @RequestMapping("/selectBySn")
    @RequirePermission(name = "预约下拉业务大类",experssion = "business:selectBySn")
    @ResponseBody
    public List<SystemDictionaryItem> selectBySn(String sn){
        List<SystemDictionaryItem> items = appointmentService.selectBySn(sn);
        return items;
    }


    @RequestMapping("/selectByBussiness")
    @RequirePermission(name = "预约下拉查看所有门店",experssion = "business:selectByBussiness")
    @ResponseBody
    public List<Business> selectByBussiness(){
        List<Business> businesses = businessService.listAll();
        return businesses;
    }


    @RequestMapping("/updateStatus")
    @RequirePermission(name = "状态的更新",experssion = "business:updateStatus")
    @ResponseBody
    public jsonResult updateStatus(Long id,Integer status){
        try {
            appointmentService.updateStatus(id,status);
        } catch (Exception e) {
            return new jsonResult(false,"操作失败");
        }
        return new jsonResult(true,"操作成功");
    }
}
